home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / dalla rivista / host contacted / ksc_grepmem.lha / GrepMem.asm < prev    next >
Assembly Source File  |  2000-02-21  |  4KB  |  221 lines

  1. ; GrepMem v1.3 : hunts through memory for (exact) string matches
  2. ; by Kyzer/CSG
  3. ; $VER: GrepMem 1.3 (21.02.00)
  4. ;
  5.     incdir    include:
  6.     include    lvo/exec_lib.i
  7.     include    lvo/dos_lib.i
  8.     include    exec/execbase.i
  9.     include    dos/dos.i
  10.     include    dos/dosasl.i
  11.     incdir    ""
  12.  
  13. error    MACRO
  14.     IFLT    ERROR_\1 - 127
  15.     moveq    #ERROR_\1,d1
  16.     ELSE
  17.     move.l    #ERROR_\1,d1
  18.     ENDC
  19.     jsr    _LVOSetIoErr(a6)
  20.     ENDM
  21.  
  22. print    MACRO
  23.     lea    \1,a0
  24.     move.l    a0,d1
  25.     IFNC    '\2',''
  26.     lea    \2,a0
  27.     move.l    a0,d2
  28.     ENDC
  29. ;    move.l    dosbase(a5),a6
  30.     jsr    _LVOVPrintf(a6)
  31.     ENDM
  32.  
  33. stackf    MACRO    ; stack_symbol, stackelement_symbol
  34.     IFND    \1
  35. \1    set    0
  36.     ENDC
  37. \1    set    \1-4
  38. \2    equ    \1
  39.     ENDM
  40.  
  41.     stackf    stk, dosbase    ; DOSBase
  42.  
  43.     stackf    stk, padzero    ;
  44.     stackf    stk, endaddr    ; these 4 filled by ReadArgs
  45.     stackf    stk, begaddr    ; ^
  46.     stackf    stk, searchstr    ; | This way up
  47.     stackf    stk, rdargs
  48. args=searchstr
  49.  
  50.     stackf    stk, end    ; end address
  51.     stackf    stk, start    ; start address 
  52.     stackf    stk, address    ; current search address
  53.     stackf    stk, hitcnt    ; number of matches
  54.     stackf    stk, column    ; current printout column
  55. str=address
  56.  
  57.     link    a5,#stk
  58.  
  59.     move.l    4.w,a6
  60.     lea    dosname(pc),a1
  61.     moveq    #37,d0
  62.     jsr    _LVOOpenLibrary(a6)
  63.     move.l    d0,dosbase(a5)
  64.     beq.s    .nodos
  65.     move.l    d0,a6
  66.  
  67.     print    title(pc)
  68.  
  69.     lea    templat(pc),a0
  70.     move.l    a0,d1
  71.     lea    args(a5),a0
  72.     move.l    a0,d2
  73.     moveq    #4-1,d3
  74. .clrarg    clr.l    (a0)+
  75.     dbra    d3,.clrarg
  76.     moveq    #0,d3
  77.     jsr    _LVOReadArgs(a6)
  78.     move.l    d0,rdargs(a5)
  79.     beq.s    .noargs
  80.  
  81.     bsr.s    Init    ; Init will call Search if appropriate
  82.  
  83.     move.l    rdargs(a5),d1
  84.     jsr    _LVOFreeArgs(a6)
  85. .noargs    jsr    _LVOIoErr(a6)
  86.     move.l    d0,d1
  87.     moveq    #0,d2
  88.     jsr    _LVOPrintFault(a6)
  89.     move.l    a6,a1
  90.     move.l    4.w,a6
  91.     jsr    _LVOCloseLibrary(a6)
  92. .nodos    unlk    a5
  93.     moveq    #0,d0
  94.     rts
  95.  
  96. Init    clr.l    hitcnt(a5)    ; hits = 0
  97.     clr.w    column(a5)    ; start at column 2 on 1st line
  98.  
  99.     lea    padpos(pc),a0    ; set up pad character
  100.     move.b    #"-",(a0)
  101.     tst.l    padzero(a5)
  102.     beq.s    1$
  103.     move.b    #"0",(a0)
  104. 1$
  105.  
  106. ; set up start and end addresses
  107.  
  108.     move.l    begaddr(a5),d0
  109.     bne.s    .got
  110.     clr.l    start(a5)
  111.     move.l    4.w,a0
  112.     move.l    MaxLocMem(a0),end(a5)
  113.     bra    Search
  114.  
  115. .got    move.l    d0,a0
  116.     bsr.s    hexconv
  117.     tst.l    d1
  118.     beq.s    .nhex
  119.     move.l    d0,start(a5)
  120.  
  121.     move.l    endaddr(a5),d0
  122.     beq.s    .noend
  123.     move.l    d0,a0
  124.     bsr.s    hexconv
  125.     tst.l    d1
  126.     beq.s    .nhex
  127.     move.l    d0,end(a5)
  128.     bra.s    Search
  129.  
  130. .nhex    error    BAD_NUMBER
  131.     rts
  132. .noend    error    REQUIRED_ARG_MISSING
  133.     rts
  134.  
  135. hexconv    include    hex.asm
  136.  
  137. ;--- Search routine ---------------------------------------------------------
  138.  
  139. Search    move.l    start(a5),a0
  140.     move.l    end(a5),a1
  141.     cmp.l    a0,a1
  142.     beq.s    .cleanup    ; if start=end then exit
  143.     bhi.s    .noswap        ; if start>end then swap start and end
  144.     exg.l    a0,a1
  145. .noswap    move.l    a0,start(a5)
  146.     move.l    a1,end(a5)
  147.  
  148.     move.l    searchstr(a5),str(a5)
  149.     print    begin(pc),str(a5)
  150.  
  151. ; search routine
  152. ; a4=string to search for
  153. ; a3=current position in memory (outer)
  154. ; a2=current position in memory (inner)
  155.  
  156.     move.l    start(a5),a3
  157.     move.l    searchstr(a5),a4
  158.     tst.b    (a4)        ; if string = '' then exit
  159.     beq.s    .cleanup
  160. .next_outer
  161.     move.b    (a4),d0
  162.     cmp.b    (a3)+,d0
  163.     bne.s    .no_start
  164.     lea    1(a4),a1    ; a1=2nd byte of search string
  165.     move.l    a3,a2        ; a2=2nd byte of matched memory
  166. .next_inner
  167.     tst.b    (a1)        ; if search string ends during inner loop
  168.     beq.s    .matched_whole    ; then we must have matched fully
  169.     cmp.b    (a1)+,(a2)+
  170.     beq.s    .next_inner
  171. .return
  172. .no_start
  173.     cmp.l    end(a5),a3
  174.     bne.s    .next_outer
  175.  
  176. .cleanup
  177.     print    done(pc),hitcnt(a5)
  178.     rts
  179.  
  180. .matched_whole
  181.     cmp.l    a3,a4
  182.     beq.s    1$        ; don't report own copy
  183.     cmp.b    #'"',-1(a3)    ; don't report if begins with quotes
  184.     beq.s    1$
  185.  
  186.     ; we got a match!
  187.  
  188.     move.l    a3,address(a5)
  189.     print    hit(pc),address(a5)
  190.     addq.l    #1,hitcnt(a5)
  191.  
  192.     ; check for CTRL-C and die if neccessary
  193.     move.l    4.w,a6
  194.     moveq    #0,d0
  195.     moveq    #0,d1
  196.     jsr    _LVOSetSignal(a6)
  197.     move.l    dosbase(a5),a6
  198.     btst.l    #SIGBREAKB_CTRL_C,d0
  199.     beq.s    2$
  200.     error    BREAK
  201.     bra.s    .cleanup
  202.  
  203. 2$    addq.w    #1,column(a5)
  204.     cmp.w    #6,column(a5)
  205.     blt.s    1$
  206.     print    newline(pc)    ; write newline every 7 columns
  207.     clr.w    column(a5)
  208. 1$    bra.s    .return        ; return to outer loop
  209.  
  210. begin    dc.b    'Grepping memory for "%s" from $%lx to $%lx:',10,0
  211. done    dc.b    10,'Grep finished, %ld hit(s).',10,0
  212. hit    dc.b    '$%-8lx   ',0
  213. newline=hit-2
  214. padpos=hit+2
  215.  
  216.  
  217.     dc.b    '$VER: '
  218. title    dc.b    'GrepMem 1.3 (21.02.00) by Kyzer/CSG',10,0
  219. templat    dc.b    'STRING/A,START,END,PADZERO/S',0
  220. dosname    dc.b    'dos.library',0
  221.